﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using BluetoothMachine.Domains;
using BluetoothMachine.Views.DialogHosts;
using LiveCharts;
using LiveCharts.Wpf;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using UIWindows.Entitys;

namespace BluetoothMachine.ViewModels
{
    public class DataReportViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }
        /// <summary>
        /// 合格率
        /// </summary>
        public int ProductionYield { get; set; }
        public AsyncObservableCollection<ProductionData> DataList { get; set; }
        #region 注液走势图
        public SeriesCollection LiquidInjectionSeriesCollection { get; set; }
        public Func<double, string> LiquidInjectionYFormatter { get; set; }

        public SeriesCollection LiquidInjectionSeriesCollection1 { get; set; }
        public Func<double, string> LiquidInjectionYFormatter1 { get; set; }
        #endregion
        /// <summary>
        /// 总计
        /// </summary>
        public int Total { get; set; }
        /// <summary>
        /// OK总计
        /// </summary>
        public int OKTotal { get; set; }
        /// <summary>
        /// NG总计
        /// </summary>
        public int NGTotal { get; set; }
        /// <summary>
        /// 配对NG总计
        /// </summary>
        public int PairTotal { get; set; }


        public DataReportViewModel()
        {
            DataList = new AsyncObservableCollection<ProductionData>();
            #region 注液量走势图
            LiquidInjectionYFormatter = val => val.ToString("0.00 ua");
            LiquidInjectionYFormatter1 = val => val.ToString("0.00 ma");
            var gradientBrush = new System.Windows.Media.LinearGradientBrush
            {
                StartPoint = new Point(0, 0),
                EndPoint = new Point(0, 1)
            };
            gradientBrush.GradientStops.Add(new GradientStop(Color.FromRgb(0x2d, 0xff, 0x70), 0));
            gradientBrush.GradientStops.Add(new GradientStop(Colors.Transparent, 1));

            var gradientBrush1 = new System.Windows.Media.LinearGradientBrush
            {
                StartPoint = new Point(0, 0),
                EndPoint = new Point(0, 1)
            };
            gradientBrush1.GradientStops.Add(new GradientStop(Color.FromRgb(0xEE, 0x99, 0xCC), 0));
            gradientBrush1.GradientStops.Add(new GradientStop(Colors.Transparent, 1));
            LiquidInjectionSeriesCollection = new SeriesCollection
            {
                new LineSeries
                {
                    Title = "静态电流",
                    Values = new ChartValues<double>(),
                    Fill = gradientBrush,
                    Stroke = new SolidColorBrush(Color.FromRgb(0x2d, 0xff, 0x70)),
                    StrokeThickness = 1,
                    PointGeometrySize = 0
                },
            };
            LiquidInjectionSeriesCollection1 = new SeriesCollection
            {
                new LineSeries
                {
                    Title = "动态电流",
                    Values = new ChartValues<double>(),
                    Fill = gradientBrush1,
                    Stroke = new SolidColorBrush(Color.FromRgb(0xEE, 0x99, 0xCC)),
                    StrokeThickness = 1,
                    PointGeometrySize = 0
                },
            };
            #endregion
            StartTime = DateTime.Now.Date;
            EndTime = DateTime.Now.Date.AddDays(1);
        }

        /// <summary>
        /// 查询
        /// </summary>
        public DelegateCommand QueryData => new DelegateCommand(async () =>
        {
            DataList.Clear();
            System.Threading.Thread.Sleep(100);
            _ = DialogHost.Show(new LoadingDialog(), "RootDialog").ConfigureAwait(false);
            await Task.Run(() =>
            {
                var list = ProductionData.FindAll(ProductionData._.TestTime >= StartTime & ProductionData._.TestTime <= EndTime);
                if (list == null) return;
                Total = list.Count;
                PairTotal = list.Count(x => x.PairNG == "是");
                OKTotal = list.Count(x => x.PairNG == "否" & x.ButtonNG == "否" & x.VoiceNG == "否" & x.Eligibility == "合格");
                NGTotal = Total - OKTotal;
                ProductionYield = (int)(OKTotal / (double)Total * 100);
                foreach (var item in list)
                {
                    DataList.Insert(0, item);
                }
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    ChartValues<double> one = new ChartValues<double>();
                    ChartValues<double> two = new ChartValues<double>();
                    one.AddRange(list.Skip(list.Count - (list.Count > 30 ? 30 : list.Count)).Select(x => x.QuiescentCurrent).ToList());//静态电流
                    two.AddRange(list.Skip(list.Count - (list.Count > 30 ? 30 : list.Count)).Select(x => x.DynamicCurrent).ToList());//动态电流
                    LiquidInjectionSeriesCollection[0].Values = one;
                    LiquidInjectionSeriesCollection1[0].Values = two;
                }));
            });
            DialogHost.Close("RootDialog");
        });

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
